package com.atguigu.guli.service.cms.controller.admin;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.cms.entity.Ad;
import com.atguigu.guli.service.cms.service.AdService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/cms/ad")
@RefreshScope //表示当前组件所使用的配置中心的配置需要动态监听

public class AdminAdController {
    @Autowired
    private AdService adService ;
    //首页banner广告的CRUD

    @PostMapping("save")
    public R save(@RequestBody Ad ad) {
        adService.save(ad) ;
        return R.ok() ;
    }

    @DeleteMapping("delete/{adId}")
    public R delete(@PathVariable String adId) {
        adService.removeById(adId) ;
        return R.ok() ;
    }

    @GetMapping("getById/{adId}")
    public R getById(@PathVariable String adId) {
        Ad ad = adService.getById(adId);
        return R.ok().data("item",ad) ;
    }

    @PutMapping("update")
    public R update(@RequestBody Ad ad) {
        adService.updateById(ad) ;
        return R.ok() ;
    }

    //分页查询广告列表
    @GetMapping("getAdPage/{pageNum}/{pageSize}")
    public R getAdPage(@PathVariable Integer pageNum,@PathVariable Integer pageSize) {
        Page<Ad> page = new Page<>(pageNum,pageSize) ;
        adService.page(page) ;
        return R.ok().data("page",page) ;
    }
}
